import request from './request'
import { useUserStore } from '../store/modules/user'
import { GetSmsSendLogListParam } from '../views/note/notelog/types'
import { AxiosPromise } from 'axios'

// 获取短信模版列表
interface getListParam {
  keyword: string
  type: '1' | '2' | '3'
  limit: number
  page: number
}
export function getSmsTemplateList(data: getListParam) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/sms/template/getList',
    method: 'post',
    headers: token,
    data,
  })
}

// 获取短信模版详情
export function getSmsTemplateDetail(id: string | number) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/system/sms/template/getDetail/${id}`,
    method: 'get',
    headers: token,
  })
}

// 启用或禁用短信模版
export function updateSmsTemplateState(param: { ids: Array<string | number>; status: boolean }) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/system/sms/template/disable?status=${param.status}`,
    method: 'post',
    headers: token,
    data: param.ids,
  })
}

// 同步短信模版
export function syncSmsTemplate() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/sms/template/syncSmsTemplate',
    method: 'get',
    headers: token,
  })
}

// 获取短信发送数量列表
export function getSendSmsCountList(data: {
  limit: number
  page: number
  sendEndTime: string
  sendStartTime: string
}) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/sms/send/log/getSendSmsCountList',
    method: 'post',
    headers: token,
    data,
  })
}

// 获取短信发送记录列表
export function getSmsSendLogList(data: GetSmsSendLogListParam) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/sms/send/log/getList',
    method: 'post',
    headers: token,
    data,
  })
}

// 根据短信ID获取发送短信详情
export function getSendLogDetail(id: number | string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/system/sms/send/log/getDetail/${id}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 根据短信模板ID获取短信接收角色类型
 * @param {String} id - 短信模板ID
 */
export function getReceiverType(id: string): AxiosPromise<SmsAPIResponse.SmsReceiverType> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/system/sms/template/getRoleType/${id}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 根据短信模板ID获取短信接收角色类型下拉数据
 * @param {String} id - 短信模板id
 */
export function getReceiverTypeOptions(
  id: string,
): AxiosPromise<SmsAPIResponse.SmsReceiverTypeOptions> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/system/select/getRoleType/${id}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 保存短信接收者类型
 * @param {String} smsTemplateId - 短信模板id
 * @param {SmsAPIRequest.SaveReceiverType} data - 请求参数
 */
export const saveSmsReceiverType = (
  smsTemplateId: string,
  data: SmsAPIRequest.SaveReceiverType,
): AxiosPromise<APIResponse> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/system/sms/template/saveRoleType/${smsTemplateId}`,
    method: 'post',
    headers: token,
    data,
  })
}
